﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ReturnPosiion : MonoBehaviour
{
    public Button rePos;//返回按键
    public RectTransform start;//中间位置
    public RectTransform m1;//面板1
    public RectTransform m2;//面板2
    Vector2 p1;
    Vector2 p2;
    // Start is called before the first frame update
    void Start()
    {
        p1 = m1.position;
        p2 = m2.position;
        rePos.onClick.AddListener(
            delegate
            {
                Homing();
            });
    }

    void Homing()
    {
        StartCoroutine(KnowPosition());
        StartCoroutine(QuitPosition());

    }

    IEnumerator KnowPosition()
    {
        Vector2 target1;
        while (Mathf.Abs(m1.position.x - start.position.x) > 10)
        {
            target1 = Vector2.Lerp
                (m1.position, start.position, 0.08f);
            target1.y = 367;
            m1.position = target1;
            yield return new WaitForFixedUpdate();
        }
        EndKnow();
    }

    void EndKnow()
    {
        StopCoroutine(KnowPosition());
    }

    IEnumerator QuitPosition()
    {
        Vector2 target2;
        while (Mathf.Abs(m2.position.x - p2.x) > 10)
        {
            target2 = Vector2.Lerp
                (m2.position, p2, 0.08f);
            target2.y = 367;
            m2.position = target2;
            yield return new WaitForFixedUpdate();
        }
        EndMove();
    }

    void EndMove()
    {
        StopCoroutine(QuitPosition());
    }

}
